home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Tstamp.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  11.5 KB  |  333 lines

  1. /*Timestamp  object with free Format output.
  2. */
  3.  
  4. package symantec.itools.awt;
  5. import java.util.*;
  6. import java.awt.*;
  7. import symjava.sql.Timestamp;
  8. public class Tstamp extends TextField{
  9.  
  10.     protected int day=1;
  11.     protected int month=0;
  12.     protected int year=0;
  13.     protected int hour=0;
  14.     protected int minute=0;
  15.     protected int second=0;
  16.     protected int nanosec=0;
  17.  
  18.     private String m_DisplayFormat="D* M* D%TH,Y*  H%:m%:s%,n3 AM";
  19.     private String m_EntryFormat="MDYhmsn";
  20.  
  21.     private String DAY[]={"DD","D%","D+","D*","TH"};
  22.     private String MON[]={"MM","M%","M+","M*"};
  23.     private String YEA[]={"YY","Y*"};
  24.     private String HOU[]={"HH","H%","hh","h%","AM"};
  25.     private String MIN[]={"mm","m%"};
  26.     private String SEC[]={"ss","s%"};
  27.     private String NAN[]={"n"};
  28.     private String nMON[]={"Jan.","Feb.","Mar.","Apr.","May ","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
  29.     private String NMON[]={"January","February","March","April","May","June","July","August","September",
  30.                            "October","November","December"};
  31.     private String nDAY[]={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};
  32.     private String NDAY[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
  33.  
  34.     String WAYS[]={"DDMMYY",
  35.                    "D% M% YY",
  36.                    "D%/M%/Y*",
  37.                    "Y*-MM-DD",
  38.                    "DD/MM/YY",
  39.                    "D%-M+-YY",
  40.                    "D% M* Y*",
  41.                    "D+ D% M+ Y*",
  42.                    "D* D% M+ Y*",
  43.                    "M* D% YY",
  44.                    "D* M* D% Y*",
  45.                    "<< M%.D%.YY >>",
  46.                    "Today is D* the D% th of M* Y*",
  47.                    "DD -- MM -- Y*"};
  48. //      CONSTRUCTORS
  49.  
  50.     public Tstamp(Date date){
  51.         this.setTstamp(date);}
  52.  
  53.     public Tstamp( symjava.sql.Timestamp tstamp){
  54.         nanosec=tstamp.getNanos();
  55.         this.setTstamp(tstamp);}
  56.  
  57.     public Tstamp()
  58.     {this(new Date());}
  59.  
  60.     public Tstamp(String date)
  61.     {this.readDate(date,"MDYhmsn");}
  62.  
  63.     public Tstamp(String date,String format)
  64.     {this.readDate(date,format);}
  65.  
  66.  
  67. //      SETTERS
  68.  
  69.     public void setTstamp(String date)
  70.     {this.readDate(date,m_EntryFormat);}
  71.  
  72.     public void setTstamp(String date, String format)
  73.     {this.readDate(date,format);}
  74.  
  75.     public void setTstamp(Date date){
  76.         day=date.getDate();
  77.         month=date.getMonth();
  78.         year=date.getYear()+1900;
  79.         hour=date.getHours();
  80.         minute=date.getMinutes();
  81.         second=date.getSeconds();
  82.     }
  83.  
  84.     public void setinequalout()
  85.     {setEntryFormat(getDisplayFormat());}
  86.  
  87.     public void setDisplayFormat(String dispfmt)
  88.     {m_DisplayFormat = dispfmt;
  89.     setinequalout();}
  90.  
  91.     public void setEntryFormat(String entfmt)
  92.     {m_EntryFormat = entfmt;}
  93.  
  94.  
  95. //      GETTERS
  96.  
  97.     public String getEntryFormat()
  98.     {return (m_EntryFormat);}
  99.  
  100.     public String getDisplayFormat()
  101.     {return (m_DisplayFormat);}
  102.  
  103.     public String getTstamp()
  104.     {return(this.exTstamp(m_DisplayFormat));}
  105.  
  106.     public String getTstamp(int num)
  107.     {return(this.exTstamp(WAYS[num]));}
  108.  
  109.     public String getTstamp(String exform)
  110.     {return(this.exTstamp(exform));}
  111.  
  112.     public void printTstamp()
  113.     {setText(getTstamp(m_DisplayFormat));}
  114.  
  115.     public Date getDate()throws IllegalArgumentException{
  116.         if(1970>year || year>2038){
  117.             throw new IllegalArgumentException("Date out of range");}
  118.     else return(new Date(year-1900,month,day,hour,minute,second));}
  119.  
  120.     public Timestamp getTimestamp()throws IllegalArgumentException{
  121.         if(1970>year || year>2038){
  122.             throw new IllegalArgumentException("Year out of range for Timestamp");}
  123.     else return(new Timestamp(year-1900,month,day,hour,minute,second,nanosec));}
  124.  
  125.  
  126.  
  127.  
  128.     protected void raiseException(String text)
  129.     {System.out.println(text);}
  130.  
  131.     protected void readDate(String date,String format){
  132.  
  133.     boolean douze=false;
  134.     String tformat="";
  135.     String sub="";
  136.     int value=0;
  137.     int pos=0;
  138.     int act=0;
  139.     int elem=0;
  140.     int count=0;
  141.     date+=" ";
  142.  
  143.     if(format.length()>2){
  144.         for(int x=0;x<format.length()-1;x++){
  145.             try{sub=format.substring(x,x+2);}
  146.             catch(StringIndexOutOfBoundsException e){};
  147.                 if(sub.equals("DD")||sub.equals("D%"))tformat+="D";
  148.                 if(sub.equals("MM")||sub.equals("M%")||sub.equals("M+")||sub.equals("M*"))tformat+="M";
  149.                 if(sub.equals("YY")||sub.equals("Y*"))tformat+="Y";
  150.                 if(sub.equals("HH")||sub.equals("H%")||sub.equals("hh")||sub.equals("h%"))tformat+="h";
  151.                 if(sub.equals("m%")||sub.equals("mm"))tformat+="m";
  152.                 if(sub.equals("s%")||sub.equals("ss"))tformat+="s";
  153.                 if(sub.charAt(0)=='n')tformat+="n";
  154.             }
  155.         if(tformat.length()>1)format=tformat;
  156.         }
  157.  
  158.     while (elem<format.length()&&pos<date.length()){
  159.  
  160.         if('0'<=date.charAt(pos) && date.charAt(pos)<='9'&& act==0){
  161.         while('0'<=date.charAt(pos) && date.charAt(pos)<='9'){
  162.             value=value*10+date.charAt(pos)-'0';
  163.             pos++;count++;act=1;
  164.  
  165.         if((format.charAt(elem)=='M' ||format.charAt(elem)=='D'||format.charAt(elem)=='h'
  166.         ||format.charAt(elem)=='m'||format.charAt(elem)=='s')&&(count>1))break;
  167.         if((format.charAt(elem)=='Y')&&(count>3))break;
  168.  
  169.         }}
  170.         if(format.charAt(elem)=='n'){
  171.             for(int j=0;j<9-count;j++){
  172.                 value=value*10;}}
  173.  
  174.         if(act==0 ){
  175.         if('0'>date.charAt(pos) || date.charAt(pos)>'9'){
  176.          for(int x=0;x<nMON.length;x++){
  177.             if(nMON[x].regionMatches(true,0,date,pos,3) && format.charAt(elem)=='M'){
  178.             value=x+1;
  179.             pos++;act=1;}
  180.          }
  181.          if("PM".regionMatches(true,0,date,pos,2)){
  182.             douze=true;
  183.             }
  184.         pos++;
  185.         }}
  186.  
  187.         if(format.charAt(elem)=='M')month=value-1;
  188.         if(format.charAt(elem)=='D')day=value;
  189.         if(format.charAt(elem)=='Y'){year=value;
  190.             if (year>=70&&year<101)year+=1900;
  191.             if (year<70)year+=2000;}
  192.         if(format.charAt(elem)=='h')hour=value;
  193.         if(format.charAt(elem)=='m')minute=value;
  194.         if(format.charAt(elem)=='s')second=value;
  195.         if(format.charAt(elem)=='n')nanosec=value;
  196.         if (douze && hour<12)hour=hour+12;
  197.         if(act!=0){elem++;count=0;act=0;value=0;}
  198.  
  199.         }
  200.         while(pos<date.length()){
  201.            if("PM".regionMatches(true,0,date,pos,2)){
  202.             if(hour<12)hour+=12;}
  203.             pos++;
  204.             }
  205.         if(month>11)month=11;
  206.         if(month<0)month=0;
  207.         if (month==1 && day>=29){
  208.             if(calculday(29,1,year)==calculday(1,2,year)){day=28;}
  209.             else{day=29;}}
  210.         else if(day>(moffset[month+1]-moffset[month]))day=(moffset[month+1]-moffset[month]);
  211.         if(day<1)day=1;
  212.         if(hour>23)hour=23;
  213.         if(hour<0)hour=0;
  214.         if(minute>59)minute=59;
  215.         if(minute<0)minute=0;
  216.         if(second>59)second=59;
  217.         if(second<0)second=0;
  218.  
  219.     }
  220.  
  221.      protected String exTstamp(String exform){
  222.         int pos=0;
  223.         int actpos=pos;
  224.         int amhour=hour;
  225.         if(hour>11)amhour=hour-12;
  226.         int nansize=0;
  227.         String snan="";
  228.         String output="";
  229.         exform+=" ";
  230.         while (pos<exform.length()){
  231.             actpos=pos;
  232.  
  233.           for(int x=0;x<DAY.length;x++){
  234.             if(DAY[x].regionMatches(0,exform,pos,DAY[x].length())){
  235.                 pos+=DAY[x].length()-1;
  236.                 if(DAY[x]=="D%")output+=Integer.toString(day);
  237.                 if(DAY[x]=="DD"){
  238.                         if(day<10)output+="0";
  239.                         output+=Integer.toString(day);}
  240.  
  241.                 if(DAY[x]=="D+")output+=nDAY[calculday(day,month,year)];
  242.                 if(DAY[x]=="D*")output+=NDAY[calculday(day,month,year)];
  243.                 if(DAY[x]=="TH"){
  244.                     if(day==1||day==21)output+="st";
  245.                     if(day==2||day==22)output+="nd";
  246.                     if(day==3||day==23)output+="rd";
  247.                     if(day>=4&&day!=21&&day!=22&&day!=23)output+="th";}
  248.                 }}
  249.  
  250.           for(int x=0;x<MON.length;x++){
  251.             if(MON[x].regionMatches(0,exform,pos,MON[x].length())){
  252.                 pos+=MON[x].length()-1;
  253.                 if(MON[x]=="M%")output+=Integer.toString(month+1);
  254.                 if(MON[x]=="MM"){
  255.                         if(month+1<10)output+="0";
  256.                         output+=Integer.toString(month+1);}
  257.                 if(MON[x]=="M*")output+=NMON[month];
  258.                 if(MON[x]=="M+")output+=nMON[month];
  259.             }}
  260.  
  261.           for(int x=0;x<YEA.length;x++){
  262.             if(YEA[x].regionMatches(0,exform,pos,YEA[x].length())){
  263.                 pos+=YEA[x].length()-1;
  264.                 if(YEA[x]=="YY")output+=Integer.toString(year-(year/100)*100);
  265.                 if(YEA[x]=="Y*")output+=Integer.toString(year);
  266.             }}
  267.           for(int x=0;x<HOU.length;x++){
  268.             if(HOU[x].regionMatches(0,exform,pos,HOU[x].length())){
  269.                 pos+=HOU[x].length()-1;
  270.                 if(HOU[x]=="h%")output+=Integer.toString(hour);
  271.                 if(HOU[x]=="hh"){
  272.                         if(hour<10)output+="0";
  273.                         output+=Integer.toString(hour);}
  274.                 if(HOU[x]=="H%")output+=Integer.toString(amhour);
  275.                 if(HOU[x]=="HH"){
  276.                         if(amhour<10)output+="0";
  277.                         output+=Integer.toString(amhour);}
  278.                 if(HOU[x]=="AM"){if(hour>11)output+="PM";
  279.                                  if(hour<12)output+="AM";}
  280.                 }}
  281.  
  282.           for(int x=0;x<MIN.length;x++){
  283.             if(MIN[x].regionMatches(0,exform,pos,MIN[x].length())){
  284.                 pos+=MIN[x].length()-1;
  285.                 if(MIN[x]=="m%")output+=Integer.toString(minute);
  286.                 if(MIN[x]=="mm"){
  287.                         if(minute<10)output+="0";
  288.                         output+=Integer.toString(minute);}
  289.  
  290.             }}
  291.            for(int x=0;x<SEC.length;x++){
  292.             if(SEC[x].regionMatches(0,exform,pos,SEC[x].length())){
  293.                 pos+=SEC[x].length()-1;
  294.                 if(SEC[x]=="s%")output+=Integer.toString(second);
  295.                 if(SEC[x]=="ss"){
  296.                         if(second<10)output+="0";
  297.                         output+=Integer.toString(second);}
  298.  
  299.             }}
  300.           for(int x=0;x<NAN.length;x++){
  301.             if(NAN[x].regionMatches(0,exform,pos,NAN[x].length())){
  302.                 pos+=NAN[x].length();
  303.                 nansize=(int)(exform.charAt(pos)-'0');
  304.                snan=Integer.toString(nanosec+1000000000);
  305.                 if(NAN[x]=="n")output+=snan.substring(1,nansize+1);
  306.             }}
  307.           if(actpos==pos)output+=exform.charAt(pos);
  308.           pos++;
  309.         }
  310.         return(output);
  311.     }
  312.  
  313.     private int moffset[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
  314.     public int calculday(int day,int month,int year){
  315.        int yearn=year-1900;
  316.        int tday=(day+4+moffset[month]+((yearn & 3) !=0||yearn%100==0&&(yearn+300)%400!=0||month<2?-1:0)
  317.                 +(yearn-70)*365
  318.                 +(yearn-69)/4
  319.                 -(yearn-1)/100
  320.                 +(yearn +299)/400);
  321.        return(tday%7);
  322.         }
  323.  
  324.         public boolean handleEvent(Event event) {
  325.         if(event.id==Event.ACTION_EVENT){
  326.             setTstamp((String)event.arg,m_EntryFormat);
  327.             printTstamp();
  328.          }
  329.         return super.handleEvent(event);
  330.     }}
  331.  
  332.  
  333.